package com.nuanshui.heatedloan.dal;

import java.util.Date;
import java.util.HashMap;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

public abstract class BaseEntityImpl implements BaseEntity {
	private static final long serialVersionUID = 1L;
	private String id;
	@JsonIgnore
	@JSONField(serialize=false)
	protected long idRaw = -1;
	@JsonInclude(Include.NON_EMPTY)
	protected Date createdAt;//插入逻辑在mysql实现，只查询
	@JsonInclude(Include.NON_NULL)
	//@JSONField(serialzeFeatures=SerializerFeature.WriteNullStringAsEmpty)
	protected Date updatedAt;//更新逻辑在mysql实现，只查询

	@Override
	public String getId() {
		if (id == null && idRaw > 0)
			id = IdTypeHandler.encode(idRaw);
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public long getIdRaw() {
		return idRaw;
	}

	public void setIdRaw(long idRaw) {
		this.idRaw = idRaw;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setUpdatedAt(Date updatedAt) {
		this.updatedAt = updatedAt;
	}

	public Date getUpdatedAt() {
		return updatedAt;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BaseEntityImpl other = (BaseEntityImpl) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public HashMap<String, Object> getExtObj() {
		return extDataObj;
	}

	public void setExtObj(HashMap<String, Object> extData) {
		this.extDataObj = extData;
	}

	public void putData(String key,Object obj)
	{
		if(extDataObj==null)
		{
			extDataObj=new HashMap<String, Object>();
		}
		extDataObj.put(key,obj);
	}

	private HashMap<String,Object> extDataObj;

}
